package ricky.loader.protocol.da
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	
	import mx.core.Application;
	
	import ricky.Interface.ILoadComplete;
	import ricky.animate.AnimScene;
	import ricky.loader.protocol.MyLoader;
	import ricky.loader.Request;

	/**
	 * da文件解码类
	 */
	public class DecoderDA
	{
		
		private var daData:DaData;
		
		private var req:Request;
		
		private var onComplete:ILoadComplete;
		
		/**
		 * 要解压的图片数量
		 */
		private var textureCount:int=0;
		
		private var textureLoaded:int=0;
		/**
		 * 解压完成  
		 */
		private function onCompelete(e:Event):void
		{ 
			var my:MyLoader=(e.currentTarget as LoaderInfo).loader as  MyLoader;
			var textureNames:String=my.tag;
			var bmp:Bitmap=my.content as Bitmap;
//			my
			daData.texttures[textureNames]=bmp.bitmapData;//	flash.display.BitmapData (@6fdfee1)	
			textureLoaded++;
			if(textureLoaded==textureCount)
				allLoaded();
		}
		/**
		 * 所有材质解压完成
		 */
		private function allLoaded():void
		{
			//fill frames
			var sName:Array=daData.sceneNames;
			var sconf:DaSceneConfig;
			for(var i:int=0;i<sName.length;i++)
			{
				sconf= daData.sceneConfigs[sName[i]];
				addFrameData(sconf);
			}
			
			toAnimScenes();
		}
		
		/**
		 * 
		 */
		private function toAnimScenes():void
		{
			var arr:Array=[];
			
			var offx:Number=daData.offx;
			var offy:Number=daData.offy;
			
			var sn:Array=daData.sceneNames;//所有场景名字数据
			var sceneData:Dictionary=daData.sceneConfigs;
			var acName:String;//原始行动名
			var soureScene:Dictionary=new Dictionary();
			for(var i:* in sn)
			{ 
				acName=sn[i];//原始场景名
				var scConfig:DaSceneConfig = sceneData[acName];
				var scene:AnimScene= AnimScene.getAnimScene(scConfig);	//创造新场景
				scene.actionName=acName;
				scene.offx=offx;
				scene.offy=offy;
				scene.model=req.key;
				arr.push(scene);
				soureScene[acName]=scene;
			}
			
			var matris:Array=daData.marixScenes;
			var matriItem:Array,source:AnimScene,ani:AnimScene;
			for(var j:* in matris)
			{ 
				matriItem=matris[j];
				source=soureScene[matriItem[1]];//原始场景
				ani=source.cloneMatrix(matriItem[0],matriItem[2]);//copy帧
				arr.push(ani);
			}
			
			daData.dispose();
			req.data=arr;
			if(onComplete!=null)
				onComplete.doComplete(req);
		}
		
		/**
		 * 添加帧数据
		 */
		private function addFrameData(sconf:DaSceneConfig):void
		{
			var total:int=sconf.totalFrame;
			sconf.frames=new Array();
			var bmpd:BitmapData,rect:Rectangle;
			var bmpSource:BitmapData=daData.texttures[sconf.texture];
			for(var i:int=0;i<total;i++)
			{
				rect=new Rectangle(i*sconf.frameWidth,sconf.rowIndex*sconf.frameHeight,sconf.frameWidth,sconf.frameHeight);
				bmpd=new BitmapData(sconf.frameWidth,sconf.frameHeight,true,0);
				bmpd.copyPixels(bmpSource,rect,new Point(0,0),bmpSource,new Point(i*sconf.frameWidth,sconf.rowIndex*sconf.frameHeight),true);
				sconf.frames.push(bmpd);
			}
//			bmpSource.dispose();
		}
		
		/**
		 * 解压.da文件 成DaData
		 * @param req:Request
		 * @param onComplete:ILoadComplete
		 * 
		 */
		public function DecoderDA(req:Request,onComplete:ILoadComplete)
		{
			daData=new DaData();			
			this.req=req;
			this.onComplete=onComplete;
			
			var byte:ByteArray=req.data;
			byte.endian=Endian.LITTLE_ENDIAN;
			daData.begin=byte.readUTF();			
			daData.offx =byte.readInt();
			daData.offy=byte.readInt();
			
			daData.textureNames=byte.readObject();//材质
			daData.textureLength=byte.readInt();//长度
			
			textureCount=daData.textureLength;
			//解压png
			var l:int;
			for(var t:int=0;t<daData.textureLength;t++)
			{
				l=byte.readInt();
				var bt:ByteArray=new ByteArray();
				byte.readBytes(bt,0,l);//读取压缩的png byteArray
				
				var load:MyLoader=new MyLoader();
				load.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompelete);
				load.tag=daData.textureNames[t];
				var loadcon:LoaderContext=new LoaderContext(false,ApplicationDomain.currentDomain,null);//指定域名	
				load.loadBytes(bt,loadcon);
			}
			
			daData.sceneNames=byte.readObject();//场景
			daData.sceneNameLength=byte.readInt();//长度		
			
			var daSconfig:DaSceneConfig;
			for(var lx:int=0;lx<daData.sceneNameLength;lx++)
			{
				daSconfig=new DaSceneConfig();	
				//trace("当前索引:"+lx+"  ... 可用字节"+byte.bytesAvailable);
				daSconfig.sceneName=byte.readUTF();
				daSconfig.texture=byte.readUTF();
				daSconfig.totalFrame=byte.readInt();
				daSconfig.speed=byte.readInt();
				daSconfig.effctFrame=byte.readInt();
				daSconfig.frameWidth=byte.readInt();
				daSconfig.frameHeight=byte.readInt();
				daSconfig.row=byte.readInt();
				daSconfig.rowIndex=byte.readInt();
				daData.sceneConfigs[daSconfig.sceneName]=daSconfig;
			}
			
			if(byte.bytesAvailable>0)
			{
				//装镜像数据
				
				l=byte.readInt();
				var sname:String,matriName:String,mpar:String,mpars:Array,mart:Matrix;
				for(var j:int=0;j<l;j++)
				{
					sname=byte.readUTF();
					matriName=byte.readUTF();
					mpar=byte.readUTF();
					if(mpar!="")
					{
						mpars=mpar.split(",");
						mart=new Matrix(int(mpars[0]),int(mpars[1]),int(mpars[2]),int(mpars[3]),int(mpars[4]),int(mpars[5]));
					}else
						mart=null;
					daData.marixScenes.push([sname,matriName,mart]);
				}
			}
			
			byte.length=0;
		}		
	}
}